<template>
	<el-dialog
		:title="title"
		:visible.sync="addComShow"
		width="50%"
		:before-close="cancel"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
	>
		<el-form label-width="150px">
			<!-- 任职 -->
			<template v-if="['任职', '任免职'].includes(title)">
				<el-form-item label="拟任职基金" required>
					<el-select v-model="form.fundSysCode" @change="changeFundType" filterable :disabled="['免职', '任免职'].includes(title)">
						<el-option
							v-for="(item, index) in fundList"
							:key="index + item.fundSysCode"
							:label="item.fundCode + '-' + item.fundFullName"
							:value="item.fundSysCode"
						>
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="拟任职人员" required>
					<el-select v-model="form.staffList" filterable multiple @change="changestaffList">
						<el-option
							v-for="(item, index) in managerList"
							:key="index + item.managerId"
							:disabled="form.dismissedManagerNameList.includes(item.managerName)"
							:label="item.managerName"
							:value="item.managerId"
						>
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="投资经理职务类型">
					<el-select v-model="form.managerJobType" filterable clearable>
						<el-option v-for="(item, index) in enumList" :key="index + 'zhiwu'" :label="item.typeName" :value="item.typeCode">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="拟任职日期" required>
					<el-date-picker
						type="date"
						placeholder="选择日期"
						v-model="form.entryNoticeDate"
						value-format="yyyy-MM-dd"
						format="yyyy-MM-dd"
					></el-date-picker>
				</el-form-item>
				<!-- <el-form-item label="任职上报董事会时间">
					<el-date-picker type="date" placeholder="选择日期" v-model="form.entryReportBodDate" value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>
				</el-form-item>
				<el-form-item label="任职董事会决议时间">
					<el-date-picker type="date" placeholder="选择日期" v-model="form.entryBodResolutionDate" value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>
				</el-form-item>
				<el-form-item label="任职报送时间">
					<el-date-picker type="date" placeholder="选择日期" v-model="form.entrySubmittedDate" value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>
				</el-form-item>
				<el-form-item label="任职协会批复时间">
					<el-date-picker type="date" placeholder="选择日期" v-model="form.entrySocietyReplyDate" value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>
				</el-form-item> -->
				<el-form-item label="上传任职扫描件">
					<el-upload
						class="upload-demo"
						action="https://jsonplaceholder.typicode.com/forms/"
						:before-remove="beforeRemove1"
						:limit="3"
						:http-request="toUploadFile"
						:on-exceed="handleExceed"
						:on-change="getValChangeyes"
						:on-preview="handlePreviewyes"
						:file-list="fileList"
					>
						<el-button size="mini" type="primary" @click="beforeUpte('任职')">点击上传</el-button>
					</el-upload>
				</el-form-item>
			</template>

			<!-- 免职 -->
			<template v-if="['免职', '任免职'].includes(title)">
				<el-form-item label="拟免职基金" required>
					<el-select v-model="form.fundSysCode" disabled>
						<el-option
							v-for="(item, index) in fundList"
							:key="index + 1 + item.fundSysCode"
							:label="item.fundCode + '-' + item.fundFullName"
							:value="item.fundSysCode"
						>
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="拟免职人员" required>
					<el-select v-model="form.dismissedManagerNameList" multiple disabled>
						<el-option
							v-for="(item, index) in managerList"
							:key="index + item.managerId"
							:label="item.managerName"
							:value="item.managerId"
						>
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="拟免职日期" required>
					<el-date-picker
						type="date"
						placeholder="选择日期"
						v-model="form.leaveNoticeDate"
						value-format="yyyy-MM-dd"
						format="yyyy-MM-dd"
					></el-date-picker>
				</el-form-item>
				<el-form-item label="离职上报董事会时间">
					<el-date-picker
						type="date"
						placeholder="选择日期"
						v-model="form.leaveReportBodDate"
						value-format="yyyy-MM-dd"
						format="yyyy-MM-dd"
					></el-date-picker>
				</el-form-item>
				<el-form-item label="离职董事会决议时间">
					<el-date-picker
						type="date"
						placeholder="选择日期"
						v-model="form.leaveBodResolutionDate"
						value-format="yyyy-MM-dd"
						format="yyyy-MM-dd"
					></el-date-picker>
				</el-form-item>
				<el-form-item label="离职报送时间">
					<el-date-picker
						type="date"
						placeholder="选择日期"
						v-model="form.leaveSubmittedDate"
						value-format="yyyy-MM-dd"
						format="yyyy-MM-dd"
					></el-date-picker>
				</el-form-item>
				<el-form-item label="离职协会批复时间">
					<el-date-picker
						type="date"
						placeholder="选择日期"
						v-model="form.leaveSocietyReplyDate"
						value-format="yyyy-MM-dd"
						format="yyyy-MM-dd"
					></el-date-picker>
				</el-form-item>
				<el-form-item label="上传离职扫描件">
					<el-upload
						class="upload-demo"
						action="https://jsonplaceholder.typicode.com/forms/"
						:before-remove="beforeRemove2"
						:limit="2"
						:http-request="toUploadFile"
						:on-exceed="handleExceed"
						:on-change="getValChangeno"
						:on-preview="handlePreviewno"
						:file-list="fileList2"
					>
						<el-button size="mini" type="primary" @click="beforeUpte('免职')">点击上传</el-button>
					</el-upload>
				</el-form-item>
			</template>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button size="small" @click="cancel">取 消</el-button>
			<el-button size="small" type="primary" @click="addConfirm">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
import { saveHrFundManager, fundStList } from '@/api/productApi/fundManager'
import { uploadFileAndSort, downloadFile } from '@/api/productApi/file'
import { timerConversion } from '@/utils/timer'
export default {
	props: {
		title: {
			type: String,
			default: ''
		},
		enumList: {
			type: Array,
			default: Array
		},
		addComShow: {
			type: Boolean,
			default: false
		},
		comIsShow: {
			type: Function,
			default: Function
		},
		queryFundManagerList: {
			type: Function,
			default: Function
		},
		fundList: {
			type: Array,
			default: Array
		}
	},
	data() {
		return {
			form: {
				fundSysCode: '',
				staffList: [],
				fileList: {},
				fileList2: {},
				dismissedManagerNameList: [],
				appointType: '',
				managerJobType: ''
			},
			fileList: [],
			fileList2: [],
			updateMsg: '',
			managerList: [],
			timerList: [
				'entryNoticeDate',
				'entryReportBodDate',
				'entryBodResolutionDate',
				'entrySubmittedDate',
				'entrySocietyReplyDate',
				'leaveNoticeDate',
				'leaveReportBodDate',
				'leaveBodResolutionDate',
				'leaveSubmittedDate',
				'leaveSocietyReplyDate'
			]
		}
	},
	mounted() {},
	methods: {
		// 任职基金change事件
		changeFundType: function() {
			this.fundList.forEach(item => {
				if (item.fundSysCode === this.form.fundSysCode) {
					// 1是公募，2是专户
					this.form.fundType = item.fundRaiseType
					// 任职基金名称
					this.form.fundName = item.fundName
				}
			})
			fundStList({ fundSysCode: this.form.fundSysCode, managerTypeList: ['2'] }).then(res => {
				if (res.msgCode === 'Y') {
					this.managerList = res.data.body
				}
			})
		},
		// 任职人员change事件
		changestaffList: function() {
			let managerIdStr = ''
			let managerNameStr = ''
			this.managerList.forEach(item => {
				for (const i in this.form.staffList) {
					if (item.managerId === this.form.staffList[i]) {
						if (managerIdStr === '') {
							managerIdStr = item.managerId
							managerNameStr = item.managerName
						} else {
							managerIdStr = managerIdStr + ',' + item.managerId
							managerNameStr = managerNameStr + ',' + item.managerName
						}
					}
				}
			})
			// 任职人员ID
			this.form.staffManagerId = managerIdStr
			// 任职人员名称
			this.form.staffManagerName = managerNameStr
		},
		handleExceed(files, fileList) {
			this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
		},
		beforeRemove1(file, fileList) {
			this.form.fileList = {}
			this.form.entryScanPiece = ''
		},
		beforeRemove2(file, fileList) {
			this.form.fileList2 = {}
			this.form.leaveScanPiece = ''
		},
		beforeUpte(type) {
			this.updateMsg = type
		},
		// 任职下载
		handlePreviewyes(file) {
			downloadFile({ fid: this.form.entryScanPiece }).then(item => {
				if (item.msgCode === 'N') {
					this.$message({ showClose: true, message: '文件不存在', type: 'error' })
					return
				}
				if (item) {
					window.open(`/update/download.do?fid=${this.form.entryScanPiece}`)
				}
			})
		},
		// 免职下载
		handlePreviewno(file) {
			downloadFile({ fid: this.form.leaveScanPiece }).then(item => {
				if (item.msgCode === 'N') {
					this.$message({ showClose: true, message: '文件不存在', type: 'error' })
					return
				}
				if (item) {
					window.open(`/update/download.do?fid=${this.form.leaveScanPiece}`)
				}
			})
		},
		// 文件值改变时触发 change事件
		getValChangeyes(file, fileList) {
			if (fileList.length > 0) {
				this.fileList = [fileList[fileList.length - 1]]
			} else {
				this.fileList = fileList[0]
			}
		},
		// 文件值改变时触发 change事件
		getValChangeno(file, fileList) {
			if (fileList.length > 0) {
				this.fileList2 = [fileList[fileList.length - 1]]
			} else {
				this.fileList2 = fileList[0]
			}
		},
		toUploadFile(params) {
			uploadFileAndSort(params).then(res => {
				if (res.msgCode === 'Y') {
					if (this.updateMsg === '任职') {
						this.form.entryScanPiece = res.obj
						this.form.fileList = { fileName: params.file.name, fileId: res }
					} else if (this.updateMsg === '免职') {
						this.form.leaveScanPiece = res.obj
						this.form.fileList2 = { fileName: params.file.name, fileId: res }
					}
				}
			})
		},
		cancel() {
			this.form = {
				dismissedManagerNameList: [],
				staffList: []
			}
			this.$emit('comIsShow')
			this.fileList = []
			this.fileList2 = []
		},
		// 新增或编辑
		addConfirm() {
			if (this.title === '任职') {
				if (!!this.form.fundSysCode && !!this.form.staffList && !!this.form.entryNoticeDate) {
					// 任职人员Array转化称字符串
					this.form.staff = this.form.staffList.toString()
					this.form.appointType = '0'
					this.save()
				} else {
					this.$message({ type: 'warning', message: '请填写任职必要字段' })
				}
			} else if (this.title === '免职') {
				if (!!this.form.fundSysCode && !!this.form.dismissedManagerNameList && !!this.form.leaveNoticeDate) {
					// 任职人员Array转化称字符串
					this.form.dismissedManagerName = this.form.dismissedManagerNameList.toString()
					this.form.appointType = '1'
					this.save()
				} else {
					this.$message({ type: 'warning', message: '请填写免职必要字段' })
				}
			} else if (this.title === '任免职') {
				if (
					!!this.form.fundSysCode &&
					!!this.form.staffList &&
					!!this.form.entryNoticeDate &&
					!!this.form.fundSysCode &&
					!!this.form.dismissedManagerNameList &&
					!!this.form.leaveNoticeDate
				) {
					// 任职人员Array转化称字符串
					this.form.staff = this.form.staffList.toString()
					this.form.dismissedManagerName = this.form.dismissedManagerNameList.toString()
					this.form.appointType = '2'
					this.save()
				} else {
					this.$message({ type: 'warning', message: '请填写任免职必要字段' })
				}
			}
		},
		// 保存
		save() {
			const data = timerConversion(2, JSON.parse(JSON.stringify(this.form)), this.timerList)
			saveHrFundManager(data).then(res => {
				if (res.msgCode === 'Y') {
					this.cancel()
					this.$emit('queryFundManagerList')
					this.$message({ type: 'success', message: res.msg })
				}
			})
		},
		edit(obj) {
			const formList = timerConversion(1, JSON.parse(obj), this.timerList)
			const oaIdStr = []
			const pkSerialStr = []
			formList.forEach(item => {
				oaIdStr.push(item.oaId)
				pkSerialStr.push(item._id)
				// 免职基金经理名称
				if (item.managerName) {
					this.form.dismissedManagerNameList.push(item.managerName)
				}
				if (item.fundSysCode) {
					this.form.fundSysCode = item.fundSysCode
				}
				if (item.fundRaiseType) {
					// 1是公募，2是专户
					this.form.fundType = item.fundRaiseType
				}
				if (item.fundName) {
					// 任职基金名称
					this.form.fundName = item.fundName
				}
			})

			// 免职基金经理对应的oaId
			this.form.dismissed = oaIdStr.join()
			// 免职主键
			this.form.dsmissedId = pkSerialStr.join()
			this.changeFundType()
		}
	}
}
</script>
<style lang="scss" scoped src='./index.scss'>
</style>
